//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "CDBLkcb.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
#define COL_DISABLE       clBtnFace
#define COL_DISABLE_TEXT  clBtnShadow
//---------------------------------------------------------------------------
// ValidCtrCheck est utilisé pour être sûr que les composants créés ne
// contiennent aucune fonction virtuelle pure.
//

static inline void ValidCtrCheck(CDBLookupComboBox *)
{
    new CDBLookupComboBox(NULL);
}
//---------------------------------------------------------------------------
__fastcall CDBLookupComboBox::CDBLookupComboBox(TComponent* Owner)
    : TDBLookupComboBox(Owner)
{
  m_asRefWebHelp = EmptyStr;
  m_Enabled = m_Visible = true;
  m_ReadOnly     = false;
  m_bBoomCrash   = true;
  m_Color        = clWindow;
  m_Font         = NULL;
  m_Label        = NULL;
  m_LibelleFont  = NULL;
  m_Libelle      = EmptyStr;
  m_SizeEspace   = 10;

  try {
    m_Font                = new TFont;
    m_Label               = new TLabel(this);
    m_LibelleFont         = new TFont;
    m_LibelleFont->OnChange = SetLibelleFontChange;
    m_Label->Transparent  = true;
    m_Label->Visible      = false;
    m_Label->Caption      = m_Libelle;
    m_Label->Enabled      = Enabled;
    m_Label->FocusControl = this;
  } catch (...) {
  }
}
//---------------------------------------------------------------------------
__fastcall CDBLookupComboBox::~CDBLookupComboBox()
{
  delete m_Font;
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::SetEnabled(bool value)
{
  m_Enabled                  = value;
  TDBLookupComboBox::Enabled = value && !m_ReadOnly;
  m_Label->Enabled           = value;
  Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::SetReadOnly(bool value)
{
  m_ReadOnly                  = value;
  TDBLookupComboBox::Enabled  = m_Enabled && !value;
  Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::SetColor(TColor value)
{
  m_Color = value;
  Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::SetFont(TFont *value)
{
  m_Font->Assign(value);
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::SetLibelle(const AnsiString value)
{
  m_Libelle        = value;
  m_Label->Caption = value; SetLibellePos();
  m_Label->Visible = (value!=EmptyStr);
  if (!m_Label->Parent) m_Label->Parent = Parent;
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::SetLibellePos()
{
  m_Label->Top  = Top + ((Height - m_Label->Height - 2) >> 1);
  m_Label->Left = Left - m_Label->Width - m_SizeEspace;
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::SetSizeEspace(int value)
{
  m_SizeEspace = value;
  SetLibellePos();
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::SetLibelleFont(TFont *value)
{
  m_LibelleFont->Assign(value);
  m_LibelleFont->PixelsPerInch = value->PixelsPerInch;
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::SetLibelleFontChange(TObject *Sender)
{
  m_Label->Font->Assign(m_LibelleFont);
  m_Label->Font->PixelsPerInch = m_LibelleFont->PixelsPerInch;
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::EvOnChange(TObject *Sender)
{
  Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::WMPaint(TWMPaint &Msg)
{
  TDBLookupComboBox::Font->Assign(m_Font);
  TDBLookupComboBox::Font->Color = (m_Enabled ? m_Font->Color : COL_DISABLE_TEXT);
  TDBLookupComboBox::Color       = ((!m_Enabled || m_ReadOnly) ? COL_DISABLE : m_Color);
  TDBLookupComboBox::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::WMWindowPosChanged(TWMWindowPosChanged &Msg)
{
  TDBLookupComboBox::Dispatch(&Msg);
  SetLibellePos();
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::WMSetFocus(TWMSetFocus  &Msg)
{
  m_bBoomCrash = true;
  TDBLookupComboBox::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::WMKillFocus(TWMKillFocus &Msg)
{
  ASSERT(dynamic_cast<TWinControl*>(Controls[0]));

  if ((Msg.FocusedWnd==((TWinControl*)Controls[0])->Handle) && m_bBoomCrash) {
    m_bBoomCrash = !m_bBoomCrash;
    ::SetActiveWindow(GetParentForm(this)->Handle);
    return;
  }

  TDBLookupComboBox::Dispatch(&Msg);
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::SetVisible(bool value)
{
  m_Visible                  = value;
  TDBLookupComboBox::Visible = value;
  m_Label->Visible           = value;
  Invalidate();
}
//---------------------------------------------------------------------------
namespace Cdblkcb
{
    void __fastcall PACKAGE Register()
    {
        TComponentClass classes[1] = {__classid(CDBLookupComboBox)};
        RegisterComponents("EIC CtrlDB", classes, 0);
    }
}
//---------------------------------------------------------------------------
void __fastcall CDBLookupComboBox::KeyDown( Word &Key, TShiftState Shift)
{
      //WebHelp
      //-------
      if( (Key == VK_F3) && (Shift.Contains( ssCtrl )) ) {
            AccesWebHelp( m_asRefWebHelp );
      }else {
          if( Key == VK_F3 ) 
            VerifierRefWebHelp( m_asRefWebHelp );
      }
}
//---------------------------------------------------------------------------
